package cn.glow.readingcloud.homepage.service.impl;


import cn.glow.readingcloud.common.pojo.IndexBooklist;
import cn.glow.readingcloud.homepage.dao.IndexBooklistMapper;
import cn.glow.readingcloud.homepage.pojo.BookList;
import cn.glow.readingcloud.homepage.pojo.BookScore;
import cn.glow.readingcloud.homepage.service.IndexBooklistItemService;
import cn.glow.readingcloud.homepage.service.IndexBooklistService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @Author Glow
 * @Date 2023-04-17 22:36:13
 * @Description 书单内图书实现接口
 * @Version 1.0
 */
@Service
public class IndexBooklistServiceImpl extends ServiceImpl<IndexBooklistMapper,IndexBooklist> implements IndexBooklistService {
    @Autowired
    private IndexBooklistMapper indexBooklistMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private IndexBooklistItemService indexBooklistItemService;
    @Override
    public BookList getIndexBooklistVO(Integer booklistId, Integer clientRandomNumber) {
       IndexBooklist indexbooklist = indexBooklistMapper.selectById(booklistId);
       BookList bookList = null;
       //是否为随机书本切换，不是则维持
       if(indexbooklist.getMoreType() == 3)
       {
           bookList = this.getRandomIndexBooklistVO(indexbooklist,clientRandomNumber);
       }
       else {
           bookList = (BookList) redisTemplate.opsForValue().get("book-list-no-random-"+booklistId);
           if(bookList == null)
           {
               //如果缓存没命中定向书单，则DB查找
               List<BookScore> list = this.indexBooklistItemService.getBooklistOrderBooks(indexbooklist.getId(), indexbooklist.getBookIds(), indexbooklist.getShowNumber(),indexbooklist.getShowLikeCount());
                    if(list.size() > 0)
                    {
                        bookList = new BookList();
                        BeanUtils.copyProperties(indexbooklist,bookList);
                        bookList.setBooks(list);
                        bookList.setRandomNumber(1);
                        redisTemplate.opsForValue().set("book-list-no-random-"+booklistId,bookList,20, TimeUnit.MINUTES);
                    }
           }
       }
        return bookList;
    }

    @Override
    public IndexBooklist getIndexBooklistById(Integer booklistId) {
        IndexBooklist indexBooklist = (IndexBooklist) redisTemplate.opsForValue().get("indexbooklist-db-"+booklistId);
                if(indexBooklist == null)
                {
                    indexBooklist = indexBooklistMapper.selectById(booklistId);
                    redisTemplate.opsForValue().set("indexbooklist-db-"+booklistId,indexBooklist);
                }
        return indexBooklist;
    }

    @Override
    public BookList getRandomIndexBooklistVO(IndexBooklist booklist, Integer clientRandomNumber) {
        Random random = new Random();
        //随机7次
        Integer randomNumber = random.nextInt(7);
        if(clientRandomNumber != null)
        {
            while (clientRandomNumber.intValue() == randomNumber.intValue())
            {
                randomNumber = random.nextInt(7);
            }
        }
        BookList bookLists =(BookList)  redisTemplate.opsForHash().get("booklist-random-booklistId-"+booklist.getId(),randomNumber.toString());
       if(bookLists == null)
       {
           //随机获取书单内的书本信息
           List<BookScore> books = indexBooklistItemService.getBooklistRandomBooks(booklist.getId(),booklist.getBookIds(),booklist.getShowNumber(),clientRandomNumber,booklist.getShowLikeCount());
            if(books.size()>0)
            {
                bookLists = new BookList();
                BeanUtils.copyProperties(booklist,bookLists);
                bookLists.setBooks(books);
                redisTemplate.opsForHash().put("booklist-random-booklistId-"+booklist.getId(),randomNumber.toString(),bookLists);
            }
       }

        return bookLists;
    }
}
